package com.gateway.video.container;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;

public abstract class AbstarctSpringModule implements ModuleLifecycle{

    @Autowired
    private ConfigurableApplicationContext parentContext;

    private ConfigurableApplicationContext applicationContext;

    protected ConfigurableApplicationContext getApplicationContext() {
        return applicationContext;
    }

    protected ConfigurableApplicationContext newApplicationContext() {
        return new ModuleApplicationContext(parentContext, getClass().getClassLoader());
    }

    @Override
    final public void onLoad() throws Throwable {
        applicationContext = newApplicationContext();

        onLoad(applicationContext);
    }

    protected void onLoad(ConfigurableApplicationContext applicationContext) {
    }

    @Override
    public final void loadCompleted() throws Throwable {
        loadCompleted(applicationContext);
    }

    protected void loadCompleted(ConfigurableApplicationContext applicationContext) throws Throwable{
    }

    @Override
    public void onUnload() throws Throwable {
        applicationContext.close();
    }
}
